﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using rock.Model;
using rock.DAL;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using rock.UI.common;

namespace rock.BLL
{
    class BllRENT_CONTRACT
    {
        DalRENT_CONTRACT dalRENT_CONTRACT = new DalRENT_CONTRACT();
        DalHOUSE_ROOM dalHOUSE_ROOM = new DalHOUSE_ROOM();
        DalRENT_CUSTOM dalRENT_CUSTOM = new DalRENT_CUSTOM();

        public void updateOrAddUser(ModelRENT_CONTRACT _ModelRENT_CONTRACT)
        {
            Hashtable sqlParamInfo = new Hashtable();

            if (dalRENT_CONTRACT.Exists(_ModelRENT_CONTRACT.SEQID))
            {
                SqlParamInfo info =  dalRENT_CONTRACT.Update(_ModelRENT_CONTRACT);
                sqlParamInfo.Add(info.sql, info.parameters);
            }
            else {
                SqlParamInfo info = dalRENT_CONTRACT.Add(_ModelRENT_CONTRACT);
                sqlParamInfo.Add(info.sql, info.parameters);
            }


            int roomStatus = -1;
            if (_ModelRENT_CONTRACT.STATUS == Constant.RENT_CONTRACT_STATUS_SHENGXIAO) {
                roomStatus = Constant.HOUSE_ROOM_STATUS_RENTED;
                
            }else if(_ModelRENT_CONTRACT.STATUS == Constant.RENT_CONTRACT_STATUS_DONGZHI){
                roomStatus = Constant.HOUSE_ROOM_STATUS_UNRENT;
            }
            else if (_ModelRENT_CONTRACT.STATUS == Constant.RENT_CONTRACT_STATUS_FEICHU)
            {
                roomStatus = Constant.HOUSE_ROOM_STATUS_UNRENT;
            }else{
                throw new Exception("合同状态错误");
            }


            //更新房间状态 
            int roomSeqid = _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID;
            String sql = "UPDATE HOUSE_ROOM SET STATUS = @STATUS WHERE SEQID = @SEQID ";
            SqlParameter[] parameters = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
            parameters[0].Value = roomStatus;
            parameters[1].Value = roomSeqid;
            sqlParamInfo.Add(sql, parameters);


            DbHelperSQL.ExecuteSqlTran(sqlParamInfo);
        }

        public void Delete(int seqid)
        {
            Hashtable sqlParamInfo = new Hashtable();

            
               ModelRENT_CONTRACT deleteObj  = dalRENT_CONTRACT.GetModel(seqid);
              
            
            //更新房间状态为未出租
            int roomSeqid = deleteObj.HOUSE_ROOM_SEQID;
            String sql = "UPDATE HOUSE_ROOM SET STATUS = @STATUS WHERE SEQID = @SEQID ";
            SqlParameter[] parameters = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
            parameters[0].Value = Constant.HOUSE_ROOM_STATUS_UNRENT;
            parameters[1].Value = roomSeqid;
            sqlParamInfo.Add(sql, parameters);


            //删除合同
            SqlParamInfo info2 = dalRENT_CONTRACT.Delete(seqid);
            sqlParamInfo.Add(info2.sql, info2.parameters);

           


            DbHelperSQL.ExecuteSqlTran(sqlParamInfo);
        }

        public void DongzhiContract(int seqid)
        {
            Hashtable sqlParamInfo = new Hashtable();


            ModelRENT_CONTRACT contractObj = dalRENT_CONTRACT.GetModel(seqid);


            //更新房间状态为未出租
            int roomSeqid = contractObj.HOUSE_ROOM_SEQID;
            String sql = "UPDATE HOUSE_ROOM SET STATUS = @STATUS WHERE SEQID = @SEQID ";
            SqlParameter[] parameters = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
            parameters[0].Value = Constant.HOUSE_ROOM_STATUS_UNRENT;
            parameters[1].Value = roomSeqid;
            sqlParamInfo.Add(sql, parameters);


            //删除合同
            //更新房间状态为未出租

            String sql2 = "UPDATE RENT_CONTRACT SET STATUS = @STATUS WHERE SEQID = @SEQID ";
            SqlParameter[] parameters2 = {
					new SqlParameter("@STATUS", SqlDbType.Int,4),
                    new SqlParameter("@SEQID", SqlDbType.Int,4)
			};
            parameters2[0].Value = Constant.RENT_CONTRACT_STATUS_DONGZHI;
            parameters2[1].Value = contractObj.SEQID;


            sqlParamInfo.Add(sql2, parameters2);





            DbHelperSQL.ExecuteSqlTran(sqlParamInfo);
        }

    }
}
